import 'package:chat/common/model/User.dart';
import 'package:chat/common/service/websocket/ChatSocketService.dart';
import 'package:chat/common/utils/storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'common/service/HttpService.dart';

class Global {
  static Future<void> init() async {
    // 确保 Flutter 环境初始化
    WidgetsFlutterBinding.ensureInitialized();

    // 初始化网络请求
    Get.put(HttpService());

    // 缓存初始化
    await Storage().init();

    UserModel? user = await Storage().getUser();
    if (user != null) {
      // ✅ 注册 WebSocket 服务
      final chatSocketService = ChatSocketService(user.uid);
      await chatSocketService.init();
      Get.put(chatSocketService);
    }

    // 状态栏、导航栏样式
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(systemNavigationBarColor: Colors.transparent));

    // EasyLoading 全局样式配置
    EasyLoading.instance
      ..indicatorType =
          EasyLoadingIndicatorType
              .fadingCircle // 指示器样式
      ..loadingStyle =
          EasyLoadingStyle
              .dark // 主题风格：dark / light
      ..indicatorSize =
          45.0 // 指示器大小
      ..radius =
          10.0 // 圆角
      ..backgroundColor = Colors.black54
      ..dismissOnTap = false; // 点击遮罩不关闭
  }
}
